package cn.toesbieya.jxc.controller.sys;

import cn.toesbieya.jxc.model.entity.SysType;
import cn.toesbieya.jxc.model.vo.R;
import cn.toesbieya.jxc.model.vo.UserVo;
import cn.toesbieya.jxc.model.vo.search.TypeSearch;
import cn.toesbieya.jxc.service.sys.SysTypeService;
import cn.toesbieya.jxc.util.SessionUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author xiao_jie
 * @date 2023/4/14 19:19
 * @describe this is a text
 */

@RestController
@RequestMapping("system/type")
public class TypeController {

    @Resource
    private SysTypeService service;


    @GetMapping("get")
    public R get() {
        return R.success(service.get());
    }

    @PostMapping("search")
    public R search(@RequestBody TypeSearch vo) {
        return R.success(service.search(vo));
    }

    @PostMapping("add")
    public R add(@RequestBody SysType type) {
        if (StringUtils.isEmpty(type.getName())) {
            return R.fail("添加失败，参数错误");
        }

        UserVo user = SessionUtil.get();

        type.setId(null);
        type.setCname(user.getNickName());
        type.setCtime(LocalDateTime.now());

        return service.add(type);
    }

    @PostMapping("update")
    public R update(@RequestBody SysType type) {
        if (StringUtils.isEmpty(type.getName()) || type.getId() == null) {
            return R.fail("修改失败，参数错误");
        }
        return service.update(type);
    }

    @PostMapping("del")
    public R del(@RequestBody SysType type) {
        if (type.getId() == null) return R.fail("删除失败，参数错误");
        return service.del(type);
    }
}
